<template>
  <div class="about">
    <div>
      <el-row>
        <el-col :span="2">
          <el-button @click="add">添加</el-button>
        </el-col>
        <el-col :span="16">
          &nbsp;
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="18">
              <el-input v-model="input" placeholder="请输入内容"></el-input>
            </el-col>
            <el-col :span="2">
              &nbsp;
            </el-col>
            <el-col :span="4">
              <el-button type="primary">搜索</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="ID" width="180">
          </el-table-column>
          <el-table-column prop="username" label="姓名" width="180">
          </el-table-column>
          <el-table-column prop="privilege" label="权限">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="success" size="mini" @click="edit(scope.row.id)">编辑</el-button>
              <el-button type="danger" size="mini" @click="dele(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </template>
      <el-pagination style="margin-top: 20px; text-align: center;" background layout="prev, pager, next" :total="1000">
      </el-pagination>

    </div>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form" :label-width="formLabelWidth">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-checkbox-group v-model="form.privileges">
            <el-checkbox :label="1">系管理</el-checkbox>
            <el-checkbox :label="2">专业管理</el-checkbox>
            <el-checkbox :label="4">班级管理</el-checkbox>
            <el-checkbox :label="8">学生管理</el-checkbox>
            <el-checkbox :label="16">教师管理</el-checkbox>
            <el-checkbox :label="32">课程管理</el-checkbox>
            <el-checkbox :label="64">选课管理</el-checkbox>
            <el-checkbox :label="128">管理员管理</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handeAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { adminList, adminDetail, adminAdd, deleteAdmin } from "@/api"
export default {
  data() {
    return {
      input: "",
      tableData: [],
      dialogFormVisible: false,
      form: {
        privileges: [],
        username: "",
        password: "",
        id: -1
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    getAdminList() {
      adminList().then(res => {
        if (res.code === 0) {
          this.tableData = res.data
        }
      })
    },
    dele(id) {
      deleteAdmin(id).then(res => {
        if (res.code === 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getAdminList()
        }
      })
    },
    // 添加
    add() {
      this.dialogFormVisible = true
    },
    // 处理 
    handeAdd() {
    // 计算权限值
    this.form.privilege = this.form.privileges.reduce((sum, privilege) => sum + privilege, 0);
    if (this.form.username && this.form.password!== 0) {
      adminAdd(this.form).then(res => {
        console.log(res);
        if (res.code === 0) {
          this.$message.success('添加成功');
          this.dialogFormVisible = false;
          this.getAdminList();
        } else {
          this.$message.error('添加失败');
        }
      }).catch(err => {
        console.error(err);
        this.$message.error('请求失败');
      });
    } else {
      this.$message.warning('请填写完整信息');
    }
  },
 
    // 编辑 先获取详情
    edit(id) {
      console.log(id);
      adminDetail(id).then(res => {
        console.log(res);
        // 数字 转成 数字 （只针对 二进制）
        let privileges = [];
        for (let bit = 0; bit < 8; bit++) {
          // 0100 0000
          // 0010 0000
          if (((1 << bit) & res.data.privilege) === 1 << bit) {
            privileges.push((1 << bit));
          }
        }
        console.log(privileges);
        
        this.form = res.data
        this.$set(this.form, "privileges", privileges)
        this.dialogFormVisible = true
      })

    }

  },
  mounted() {
    this.getAdminList()
  }
}
</script>
